package com.xdap.pg.exam.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.definesys.mpaas.common.exception.MpaasBusinessException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xdap.common.entity.CheckResult;
import com.xdap.pg.exam.dto.ExamQuestionDto;
import com.xdap.pg.exam.response.Response;
import com.xdap.pg.exam.service.ExamService;
import com.xdap.pg.exam.vo.CheckQuestionsVo;
import com.xdap.runtime.service.RuntimeTokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/custom/exam")
public class ExamController {

    @Resource ExamService examService;
    @Resource ObjectMapper objectMapper;
    @Resource RuntimeTokenService runtimeTokenService;

    @PostMapping("/extractQuestions")
    public ResponseEntity extractQuestions (@Header(value = "token") String token, @RequestBody String jsonData) {
        log.info("传入的参数为: {}", jsonData);
        jsonData = jsonData.replace("\"[","[").replace("]\"","]").replace("\\","");
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Object object = jsonObject.get("area");
        if (Objects.nonNull(object)) {
            if (object instanceof String) {
                if (StringUtils.isBlank(object.toString())) {
                    log.error("传过来的area为空,请检查");
                    throw new MpaasBusinessException("传过来的area为空,请检查");
                } else {
                    log.trace("传过来的area: {},为单个字符串,需要转换成数组");
                    jsonObject.put("area", Arrays.asList(object.toString()));
                }
            } else if (object instanceof List) {
                log.trace("传过来的area: {},为数组,不需要转换");
            }
        }
        jsonData = jsonObject.toJSONString();
        log.info("转换过的抽题的参数为: {}", jsonData);
        ExamQuestionDto examQuestionDto = null;
        try {
            examQuestionDto = objectMapper.readValue(jsonData, ExamQuestionDto.class);
            if (Objects.nonNull(examQuestionDto)) {
                if (StringUtils.isBlank(examQuestionDto.getSiteName())) {
                    log.error("传过来的siteName为空,请检查");
                    throw new MpaasBusinessException("传过来的siteName为空,请检查");
                }
                if (StringUtils.isBlank(examQuestionDto.getVersion())) {
                    log.error("传过来的version为空,请检查");
                    throw new MpaasBusinessException("传过来的version为空,请检查");
                }
                if (Objects.isNull(examQuestionDto.getArea()) || examQuestionDto.getArea().isEmpty()) {
                    log.error("传过来的area为空,请检查");
                    throw new MpaasBusinessException("传过来的area为空,请检查");
                }
                Response response = examService.extractQuestions(examQuestionDto);
                return ResponseEntity.ok(response);
            } else {
                log.error("传过来的值转换的对象为空，请检查");
                throw new MpaasBusinessException("传过来的值转换的对象为空，请检查");
            }
        } catch (JsonProcessingException e) {
            log.error("Json Processing Exception", e);
            throw new MpaasBusinessException("传过来的值转换的对象为空，请检查");
        }
    }

    /**
     * 试题提交前校验
     */
    @PostMapping("/checkSubmitQuestions")
    public com.definesys.mpaas.common.http.Response checkSubmitQuestions(@RequestBody CheckQuestionsVo checkQuestionsVo){
        return examService.checkSubmitQuestions(checkQuestionsVo);
    }
}
